/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controladores;

import Clases.Alumno;
import Clases.Alumnonota;
import Clases.AlumnonotaPK;
import Clases.Nota;
import escuela.exceptions.NonexistentEntityException;
import escuela.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Bryan
 */
public class AlumnonotaJpaController {

    public AlumnonotaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Alumnonota alumnonota) throws PreexistingEntityException, Exception {
        if (alumnonota.getAlumnonotaPK() == null) {
            alumnonota.setAlumnonotaPK(new AlumnonotaPK());
        }
        alumnonota.getAlumnonotaPK().setIdNota(alumnonota.getNota().getIdNota());
        alumnonota.getAlumnonotaPK().setIdAlumno(alumnonota.getAlumno().getIdAlumno());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Alumno alumno = alumnonota.getAlumno();
            if (alumno != null) {
                alumno = em.getReference(alumno.getClass(), alumno.getIdAlumno());
                alumnonota.setAlumno(alumno);
            }
            Nota nota = alumnonota.getNota();
            if (nota != null) {
                nota = em.getReference(nota.getClass(), nota.getIdNota());
                alumnonota.setNota(nota);
            }
            em.persist(alumnonota);
            if (alumno != null) {
                alumno.getAlumnonotaCollection().add(alumnonota);
                alumno = em.merge(alumno);
            }
            if (nota != null) {
                nota.getAlumnonotaCollection().add(alumnonota);
                nota = em.merge(nota);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAlumnonota(alumnonota.getAlumnonotaPK()) != null) {
                throw new PreexistingEntityException("Alumnonota " + alumnonota + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Alumnonota alumnonota) throws NonexistentEntityException, Exception {
        alumnonota.getAlumnonotaPK().setIdNota(alumnonota.getNota().getIdNota());
        alumnonota.getAlumnonotaPK().setIdAlumno(alumnonota.getAlumno().getIdAlumno());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Alumnonota persistentAlumnonota = em.find(Alumnonota.class, alumnonota.getAlumnonotaPK());
            Alumno alumnoOld = persistentAlumnonota.getAlumno();
            Alumno alumnoNew = alumnonota.getAlumno();
            Nota notaOld = persistentAlumnonota.getNota();
            Nota notaNew = alumnonota.getNota();
            if (alumnoNew != null) {
                alumnoNew = em.getReference(alumnoNew.getClass(), alumnoNew.getIdAlumno());
                alumnonota.setAlumno(alumnoNew);
            }
            if (notaNew != null) {
                notaNew = em.getReference(notaNew.getClass(), notaNew.getIdNota());
                alumnonota.setNota(notaNew);
            }
            alumnonota = em.merge(alumnonota);
            if (alumnoOld != null && !alumnoOld.equals(alumnoNew)) {
                alumnoOld.getAlumnonotaCollection().remove(alumnonota);
                alumnoOld = em.merge(alumnoOld);
            }
            if (alumnoNew != null && !alumnoNew.equals(alumnoOld)) {
                alumnoNew.getAlumnonotaCollection().add(alumnonota);
                alumnoNew = em.merge(alumnoNew);
            }
            if (notaOld != null && !notaOld.equals(notaNew)) {
                notaOld.getAlumnonotaCollection().remove(alumnonota);
                notaOld = em.merge(notaOld);
            }
            if (notaNew != null && !notaNew.equals(notaOld)) {
                notaNew.getAlumnonotaCollection().add(alumnonota);
                notaNew = em.merge(notaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                AlumnonotaPK id = alumnonota.getAlumnonotaPK();
                if (findAlumnonota(id) == null) {
                    throw new NonexistentEntityException("The alumnonota with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(AlumnonotaPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Alumnonota alumnonota;
            try {
                alumnonota = em.getReference(Alumnonota.class, id);
                alumnonota.getAlumnonotaPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The alumnonota with id " + id + " no longer exists.", enfe);
            }
            Alumno alumno = alumnonota.getAlumno();
            if (alumno != null) {
                alumno.getAlumnonotaCollection().remove(alumnonota);
                alumno = em.merge(alumno);
            }
            Nota nota = alumnonota.getNota();
            if (nota != null) {
                nota.getAlumnonotaCollection().remove(alumnonota);
                nota = em.merge(nota);
            }
            em.remove(alumnonota);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Alumnonota> findAlumnonotaEntities() {
        return findAlumnonotaEntities(true, -1, -1);
    }

    public List<Alumnonota> findAlumnonotaEntities(int maxResults, int firstResult) {
        return findAlumnonotaEntities(false, maxResults, firstResult);
    }

    private List<Alumnonota> findAlumnonotaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Alumnonota.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Alumnonota findAlumnonota(AlumnonotaPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Alumnonota.class, id);
        } finally {
            em.close();
        }
    }

    public int getAlumnonotaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Alumnonota> rt = cq.from(Alumnonota.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
